/**
 * 
 */
package com.novartis.nims.imagedc.application;

import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.novartis.nims.imagedc.logic.Scan;

/**
 * @author snydejo2
 *
 */
public class ImageDisplaySelectionHandler implements ListSelectionListener {
	
	private ScanTableModel model;
	private ScanReviewPanel scanReviewPanel;
	
	public ImageDisplaySelectionHandler(ScanTableModel theModel, ScanReviewPanel panel) {
		model = theModel;
		scanReviewPanel = panel;
	}

	/* (non-Javadoc)
	 * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
	 */
	public void valueChanged(ListSelectionEvent event) {
		ListSelectionModel selection = (ListSelectionModel)event.getSource();
		
		if (! selection.isSelectionEmpty()) {
			int minIndex = selection.getMinSelectionIndex();
			int maxIndex = selection.getMaxSelectionIndex();
			
			int finalIndex = -1;
			
			for (int i = minIndex; i <= maxIndex; i++) {
				if (selection.isSelectedIndex(i)) {
					finalIndex = i;
				}
			}
			
			if (finalIndex != -1) {
				Scan scan = model.getScanAt(finalIndex);
				scanReviewPanel.setScan(scan);
			}
		}
	}

}
